package cn.jzx.scalademo1

object Demo1 extends App {


  /*
    //1.
    var s = Set("ab","aaa", "yz")
    var t = Set("ab", "gh", "mn", "st", "yz" )
    //求交集
    println(t & s)

    println(t intersect s)
    //求并集
    println(t | s)
    t union s
    //求差集
    println(t &~ s)
    t diff s
  */


  /* //2.
   var map =  Map("a"->1,"b"->2,"c"->3,"d"->4,"e"->5,"f"->6,"g"->7,"h"->8,"i"->8)
    println(map)
    println(map.getOrElse("a","buzhidao"))
    println(map("a"))
    println(map-="a")
    println(map)
   map += "a" -> 1
    println(map)*/


  //3.
  val line1 = "The Apache Hive ™ data warehouse software facilitates reading, writing, and managing large datasets residing in distributed storage using SQL. Structure can be projected onto data already in storage. A command line tool and JDBC driver are provided to connect users to Hive"
  //reduceByKey

  //  line1.split(" ").foldLeft(scala.collection.mutable.Map.empty[String, Int])((m, x) => {
  //    m += (x -> (m.getOrElse(x, 0) + 1))
  //    m
  //  }) foreach (println(_))

  //  val arr = line1.split(" ")
  //  println(arr.toList)

  val line2 = "i am student i am 17 i am boy"
  val arr = Array(line1, line2)
  arr.flatMap(line => line.split(" ")).foreach(println(_))

  //java String equals indexOf startWith endWith replace replaceAll substring concat ...

  //1 分组 groupBy(value) value 就将来分完组的key
  //arr.groupBy(a => a) //k/v
  //2 聚合想法
  //val m:Map[String,Int] = Map()

  import scala.collection.mutable.{Map => MMap}

  def f1(map: MMap[String, Int], word: String): MMap[String, Int] = {
    map += word -> (map.getOrElse(word, 0) + 1)
    map
  }

  def f2(m: MMap[String, Int], w: String) = {
    m += w -> (m.getOrElse(w, 0) + 1)
    m
  }

  arr.foldLeft(MMap[String, Int]())((m, w) => {
    m += w -> (m.getOrElse(w, 0) + 1)
    m
  }).foreach(println(_))


}
